/*
* ==========================================业务逻辑层==========================================
* ===================================配件信息、仓库、日志数据块===================================
* 1. 配件信息类：包含配件的编号、名称、型号、价格、位置、库存数量、库存上限、库存下限
* 2. 仓库记录基类：包含仓库记录的编号、配件编号、操作时间、操作状态
* 3. 仓库入库信息记录类：派生自仓库记录基类，增加入库数量
* 4. 仓库出库信息记录类：派生自仓库记录基类，增加出库数量
* 5. 仓库异常信息记录类：包含异常编号、配件编号、实际数量、系统数量、差异描述、确认状态
* 6. 日志记录类：包含日志编号、操作时间、操作人角色编号、操作描述
* ===================================配件信息、仓库、日志数据块===================================
* ==========================================业务逻辑层==========================================
*/

#pragma once

#include <iostream>
#include <string>
#include <ctime>
#include <sstream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <memory>
#include "User.h"
#include "PartStore.h"
#include "infoManagerTemplate.hpp"

/*============================================<管理基类实现>============================================*/
class BaseManager
{
private:
    MYPTR_T user;//已经登陆的用户信息
    std::vector<MYPTR_T> loginLib;
public:
    int load_UserFromFile();
    MYPTR_T& getUser(void);
    void setUser(const MYPTR_T& user);

    MYPTR_T login_verification(const std::string& name, const std::string& password);//登录校验
};

/*============================================<管理人员管理类实现>============================================*/
class UserManager:public BaseManager//管理员角色
{
private:
    std::vector<MYPTR_T> userLib;
public:
    int load_UserFromFile();
    int save_UserToFile();
    int info_sort();
    int show_UserInfo();

    int add_info(User& user, Role role);
    int delete_info(int id);
    int modify_info(int id,const User& user);
    MYPTR_T find_info(int id);
};


/*============================================<配件信息管理类实现>============================================*/
class PartManager//配件信息录入人员
{
private:
    std::vector<std::shared_ptr<Part>> partLib;
public:
    std::vector<std::shared_ptr<Part>>& getPartLib();
    void setPartLib(std::vector<std::shared_ptr<Part>> partLib);
    
    int load_PartFromFile();
    int save_PartToFile();
    int info_sort();
    int show_PartInfo();
    int show_not_in_store_info();
    int assigning_partLocation(int partId, std::string location);

    int add_info(Part& part);
    int delete_info(int id);
    int modify_info(int id, const Part& part);
    int modify_info(int id, std::string location, const Part& part);
    std::shared_ptr<Part> find_info(int id);
    std::shared_ptr<Part> find_info(int id, std::string location);
    int find_info_all(int id);
};


/*============================================<仓库入库信息管理类实现>============================================*/
class StoreInManager//仓库管理人员（StoreIO）
{
private:
    std::vector<std::shared_ptr<StoreInRecord>> storeInInfoLib;
public:
    int load_StoreFromFile();
    int save_StoreToFile();

    int add_info(StoreInRecord& storeInfo);
    int delete_info(int id);
    int modify_info(int id, const StoreInRecord& storeRecord);
    std::shared_ptr<StoreInRecord> find_info(int id);

    int get_record_endId(void);

    int show_StoreInfo();
};


/*============================================<仓库出库信息管理类实现>============================================*/
class StoreOutManager//仓库管理人员（StoreIO）
{
private:
    std::vector<std::shared_ptr<StoreOutRecord>> storeOutInfoLib;
public:
    int load_StoreFromFile();
    int save_StoreToFile();

    int add_info(StoreOutRecord& storeInfo);
    int delete_info(int id);
    int modify_info(int id, const StoreOutRecord& storeRecord);
    std::shared_ptr<StoreOutRecord> find_info(int id);

    int get_record_endId(void);
    
    int show_StoreInfo();
};


/*============================================<仓库出入库管理类实现>============================================*/
class StoreBaseManager :public PartManager, public StoreInManager, public StoreOutManager//仓库出入库类，负责联动配件信息、出入库信息
{
private:

public:

    int store_out(std::vector<std::shared_ptr<Part>>& partLib, int id, std::string location, int outQuantity);
    int store_in(std::vector<std::shared_ptr<Part>>& partLib, int id, std::string location, int inQuantity);
};


/*============================================<仓库盘点信息管理类实现>============================================*/

class StoreCompare
{
public:
    int partId;
    std::string location;
    int realQuantity;

    StoreCompare() = default;
    StoreCompare(int partId, std::string location, int realQuantity) :partId(partId), location(location), realQuantity(realQuantity) {}
};

class StoreAnomalyManager :public PartManager//仓库盘点人员
{
private:
    std::vector<std::shared_ptr<StoreAnomaly>> currentAnomalyLib;
    std::vector<std::shared_ptr<StoreAnomaly>> allAnomalyLib;
    
public:
    int load_CurrentStoreAnomalyFromFile();
    int load_AllStoreAnomalyFromFile();
    int save_CurrentStoreAnomalyToFile();
    int save_AllStoreAnomalyToFile();

    int show_CurrentStoreAnomaly();
    int show_AllStoreAnomaly();

    int add_info(StoreAnomaly& anomaly);
    int delete_info(int id);
    int modify_info(int id, const StoreAnomaly& storeAnomaly);
    std::shared_ptr<StoreAnomaly> find_info(int id);
    std::shared_ptr<StoreAnomaly> find_info_current(int id);

    int get_all_anomaly_endId(void);
    int get_current_anomaly_endId(void);

    int compare_to_lib(std::vector<std::shared_ptr<Part>>& partLib, std::vector <std::shared_ptr<StoreCompare>> compareLib, int checkCount);//盘点操作
    int confirm_differences(std::vector<std::shared_ptr<Part>>& partLib);
    int confirm_differences(std::vector<std::shared_ptr<Part>>& partLib, int confirmCount, std::vector<int> confirmIdLib);

};


/*============================================<日志信息管理类实现>============================================*/
class LogManager//管理员角色
{
private:
    std::vector<std::shared_ptr<LogEntry>> logLib;

public:
    int load_logFormFile();
    int save_logToFile();

    int get_log_endId(void);
    
    int out_log(std::string operatorName, Role role, std::string operationDescribe);
    int show_log();
    int find_log(int logId);
    int find_log(std::string operatorName);
    int find_log(Role role);
    int find_log(myTime_t operationTimeStart, myTime_t operationTimeEnd);
};